var mongodb = require('./db');

var Comment = function(comment){
	this.name = comment.name,
	this.day = comment.day,
	this.title = comment.title,
	this.comment = comment.comment
	console.log(this.comment);
	//获取服务器时间,为评论添加时间
	var date = new Date();
	this.comment.time = date.getFullYear()+'-'+(date.getMonth()+1)+'-'+date.getDate()+" "+date.getHours()+":"
			+(date.getMinutes()<10?"0"+date.getMinutes():date.getMinutes());

}
module.exports= Comment;

//存储一条留言信息
Comment.prototype.save = function(callback) {
	var query = {
		'name': this.name,
		'title': this.title,
		'time.day': this.day
	};
	var comment = this.comment;
	mongodb.open(function(err,db){
		if(err){
			return callback(err);
		}
		db.collection('blogs',function(err,blogs){
			if(err){
				mongodb.close();
				return callback(err);
			}
			console.log(JSON.stringify(this.comment));
			blogs.update(query,{$push:{'comments':comment}},function(err){
				mongodb.close()
				if(err){
					return callback(err);
				}
				callback(null);
			});
		});
	});
};
